﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.BL;
using SES.CMS.DO;
using System.Data;

namespace SES.CMS.AdminCP
{
    public partial class ListProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Functions.ddlDatabinder(ddlParentCate, cmsProductCategoryDO.PRODUCTCATEGORYID_FIELD, cmsProductCategoryDO.PRODUCTCATEGORYTITLE_FIELD, new cmsProductCategoryBL().SelectAll());
                grvProductDataSource(0);
            }
        }
        protected void grvProductDataSource(int productCategoryID)
        {
            
            if (int.Parse(ddlParentCate.SelectedValue) == 0)
            {
                grvProduct.DataSource = new cmsProductBL().SelectAll();
                grvProduct.DataBind();
            }
            else
            {
                grvProduct.DataSource = new cmsProductBL().SelectByProductCategory(productCategoryID);
                grvProduct.DataBind();
            }
        }
        protected void ddlParentCate_SelectedIndexChanged(object sender, EventArgs e)
        {
            int productCategoryID = int.Parse(ddlParentCate.SelectedValue);
            if (int.Parse(ddlParentCate.SelectedValue) == 0)
            {
                grvProduct.DataSource = new cmsProductBL().SelectAll();
                grvProduct.DataBind();
            }
            else {
                grvProductDataSource(productCategoryID);
            }
        }
        public DataTable ProductPropertiesDataSource(int productID)
        {
            return new cmsProductPropertyBL().SelectByProductID(productID);
        }
        protected void grvProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ArticleID = int.Parse(grvProduct.DataKeys[grvProduct.SelectedIndex].Value.ToString());
            Response.Redirect("Product.aspx?ProductID=" + ArticleID.ToString());
        }
        protected void grvProduct_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int productID = Convert.ToInt32(grvProduct.DataKeys[e.RowIndex].Value);
            new cmsProductBL().Delete(new cmsProductDO { ProductID = productID });
            new cmsProductPropertyBL().DeleteByProductID(productID);
            Functions.Alert("Xóa bản tin thành công!", Request.Url.ToString());
        }
        protected void grvProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvProduct.PageIndex = e.NewPageIndex;
            if (ddlParentCate.SelectedValue.Equals("0"))
                grvProductDataSource(0);
            else
                grvProductDataSource(int.Parse(ddlParentCate.SelectedValue));
        }
    }
}